package com.combetter.changxing.http.cookies;

import android.util.Log;



import java.io.IOException;
import java.util.HashSet;

import okhttp3.Interceptor;
import okhttp3.Response;

/**
 * Author: qintao
 * E-mail: 954280376@qq.com
 * Date: 2018/3/23 10:46
 * Description: ReceivedCookiesInterceptor
 */

public class ReceivedCookiesInterceptor implements Interceptor {
    private static final String TAG = "SmartHttp";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Log.d(TAG, "ReceivedCookiesInterceptor start");
        Response originalResponse = chain.proceed(chain.request());
        //正常保存cookie方式
        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            HashSet<String> cookies = new HashSet<>();

            for (String header : originalResponse.headers("Set-Cookie")) {
                cookies.add(header);
            }
            //sp是android的一种key,value的缓存机制
//            SPUtils.getInstance().put("cookies", cookies);
        }

        //能联网保存cookie方式
//        StringBuffer stringBuffer = new StringBuffer();
//        for(String header : originalResponse.headers("Set-Cookie")){
//            stringBuffer.append(header).append(";");
//        }
//        if(!TextUtils.isEmpty(stringBuffer.toString()) && stringBuffer.toString().contains("session.id")){
//            SPUtils.getInstance().put("cookies",stringBuffer.toString());
//        }
        Log.d(TAG, "ReceivedCookiesInterceptor end");
        return originalResponse;
    }
}